//
//  HMWelcomeViewController.swift
//  WeiBo
//
//  Created by Apple on 2017/5/31.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
import SnapKit

//  欢迎页面
class HMWelcomeViewController: UIViewController {

    //  MARK:   --懒加载
    fileprivate lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //  用户头像
    fileprivate lazy var userImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //  圆形显示
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        return imageView
    }()
    //  用户信息
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel()
        if let name = HMUserAccountViewModel.sharedUserAccount.userAccount?.name {
            label.text = "欢迎回来，\(name)"
        } else {
            label.text = "欢迎回来!"
        }
        
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
        label.textAlignment = .center
        //  默认透明度
        label.alpha = 0
        return label
    }()
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
    }
    
    //  视图已经显示完成
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }

    //  MARK:   --添加控件设置约束
    fileprivate func setupUI() {
        view.addSubview(userImageView)
        view.addSubview(messageLabel)
        userImageView.snp.makeConstraints { (make) in
            make.top.equalTo(view).offset(200)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(userImageView)
            make.top.equalTo(userImageView.snp.bottom).offset(10)
        }
    }
    
    //  MARK:   --开启动画
    fileprivate func startAnimation() {
        //  修改约束
        userImageView.snp.updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        //  弹簧动画
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: [], animations: {
            self.view.layoutIfNeeded()
        }) { (_) in
            UIView.animate(withDuration: 1, animations: { 
                self.messageLabel.alpha = 1
            }, completion: { (_) in
                //  切换根视图控制器
                NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotificationName), object: nil)
            })
        }
    }
    
}
